package flink.examples;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

public class WindowAggregateDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env =
                StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> sensorDS = env
                .socketTextStream("192.168.111.128", 7000)
                .map(new WaterSensorMapFunction());
        KeyedStream<WaterSensor, String> sensorKS =
                sensorDS.keyBy(sensor -> sensor.getId());
// 1. 窗口分配器
        WindowedStream<WaterSensor, String, TimeWindow> sensorWS =
                sensorKS.window(TumblingProcessingTimeWindows.of(Time.seconds(10)));
        SingleOutputStreamOperator<String> aggregate = sensorWS
                .aggregate(
                        new AggregateFunction<WaterSensor, Integer,
                                String>() {
                            @Override
                            public Integer createAccumulator() {
                                System.out.println("创建累加器");
                                return 0;
                            }

                            @Override
                            public Integer add(WaterSensor value,
                                               Integer accumulator) {
                                System.out.println(" 调 用 add 方法,value=" + value);
                                return accumulator + value.getVc();
                            }

                            @Override
                            public String getResult(Integer
                                                            accumulator) {
                                System.out.println("调用 getResult 方法");
                                return accumulator.toString();
                            }

                            @Override
                            public Integer merge(Integer a, Integer
                                    b) {
                                System.out.println("调用 merge 方法");
                                return null;
                            }
                        }
                );
        aggregate.print();
        env.execute();
    }
}